using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Article : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadCombos();
            if (Request.QueryString["id"] != null)
            {
                loadData();
            }
        }
    }

    private void loadData()
    {
        DataTable dt = new DataTable();
        DAL dal = new DAL();
        int intArticle_Id=0;
        try{intArticle_Id=int.Parse(Request.QueryString["ID"]);}
        catch { throw new Exception("Invalid Page Id"); }
        dt = dal.getArticle(Request.QueryString["ID"]);
        if(dt.Rows.Count>0)
        {
            txtArticle_Id.Value = intArticle_Id.ToString();
            try{cmbPages.Items.FindByValue(dt.Rows[0]["Pages_Id"].ToString()).Selected=true;}
            catch{cmbPages.SelectedIndex=0;}
            txtTitle.Value=dt.Rows[0]["Title"].ToString();
            richtext.InnerHtml=dt.Rows[0]["Content"].ToString();
            txtDate_Added.Value=DateTime.Parse(dt.Rows[0]["Date_Added"].ToString()).ToString("yyyy-MM-dd");
        }
    }

    private void loadCombos()
    {
        DataTable dt = new DataTable();
        DAL dal = new DAL();
        dt = dal.getPages();
        if (dt.Rows.Count != 0)
        {
            cmbPages.DataSource = dt;
            cmbPages.DataTextField = "Menu_Title";
            cmbPages.DataValueField = "pages_id";
            cmbPages.DataBind();
            cmbPages.Items.Insert(0,new ListItem("Select Page","0"));
        }
        else
        {
            cmbPages.Items.Clear();
            cmbPages.Items.Add(new ListItem("Please add Pages", "0"));
        }
    }
}
